package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.service.UploadService;
import com.micro.plugins.oss.manager.OssExecuteManager;
import com.micro.plugins.oss.vo.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020/5/9
 */
@Service("uploadService")
public class UploadServiceImpl implements UploadService {

    @Autowired
    private OssExecuteManager ossExecuteManager;

    /**
     * <h2>图片上传</h2>
     * @param files             上传的文件
     * @param uploadType        上传的类型
     * @param request           http请求对象
     * @param response          http返回对象
     * @return
     * @throws IOException
     */
    @Override
    public RespJsonData uploadImage(MultipartFile[] files, String uploadType, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<String> filePath = new ArrayList<String>();
        for(MultipartFile file:files){
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            UploadFile uploadFile = new UploadFile();
            uploadFile.setUploadData(file.getBytes());
            uploadFile.setSuffix(suffix);
            uploadFile.setUploadType(uploadType);
            String url = ossExecuteManager.computeRule("qiniu-cloud",uploadFile);
            filePath.add(url);
        }
        return new RespJsonData(filePath, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }
}
